package com.ruoyi.quartz.task;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.extra.pinyin.PinyinUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.cinema.DTO.*;
import com.ruoyi.cinema.domain.*;
import com.ruoyi.cinema.service.*;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.utils.YzResult;
import com.ruoyi.utils.YzUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

@Component("CinemaTask")
public class CinemaTask {
    public Integer SUCCESS = 200;
    public String BIZSUCCESS = "SUCCESS";

    private static final Logger logger = LoggerFactory.getLogger(CinemaTask.class);


    @Autowired
    private IAreaService areaService;
    @Autowired
    private ICinemaService cinemaService;
    @Autowired
    private IFilmService filmService;
    @Autowired
    private IFilmTypeService filmTypeService;
    @Autowired
    private IFilmStillService filmStillService;
    @Autowired
    private IFilmScheduleService filmScheduleService;
    @Autowired
    private IScheduleAreaInfoService scheduleAreaInfoService;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private IGoodsCategoryService goodsCategoryService;
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IComboGroupGoodsService comboGroupGoodsService;
    @Autowired
    private IComboGroupInfoService comboGroupInfoService;
    @Autowired
    private ISchedulePromoService schedulePromoService;
    @Autowired
    private ISchedulePromoAreaService schedulePromoAreaService;
    @Autowired
    private ISchedulePromoCardInfoService schedulePromoCardInfoService;




    public void syncCinema() {
       YzResult<CinemaYzDTO> result =  YzUtils.getCinemaList();
       if(null != result.getResultCode() && result.getResultCode().equals(SUCCESS)){
           if(result.getData().getBizCode().equals(BIZSUCCESS)){
               List<CinemaYzDTO> cinemaYzDTOList = result.getData().getBizDataList();
               List<Area> areaList = areaService.selectAreaList(new Area());
               Map<String,Area> areaCodeMap = areaList.stream().collect(Collectors.toMap(Area::getCode, area -> area));
               if(null != cinemaYzDTOList && !cinemaYzDTOList.isEmpty()){
                   for (CinemaYzDTO cyd :cinemaYzDTOList) {
                       if(StringUtils.isNotEmpty(cyd.getProvinceCode()) && null == areaCodeMap.get(cyd.getProvinceCode())){
                           Area area = new Area();
                           area.setCode(cyd.getProvinceCode());
                           area.setName(cyd.getProvinceName());
                           if(StringUtils.isNotEmpty(cyd.getProvinceName())){
                               area.setPinyin(PinyinUtil.getPinyin(cyd.getProvinceName()));
                               area.setPre(StringUtils.upperCase(String.valueOf(PinyinUtil.getFirstLetter(cyd.getProvinceName().charAt(0)))));
                           }
                           area.setType(String.valueOf(AreaTypeEnum.province.getValue()));
                           area.setParentId(0L);
                           areaService.insertArea(area);
                           areaCodeMap.put(cyd.getProvinceCode(),area);
                       }
                       if(StringUtils.isNotEmpty(cyd.getCityCode()) && null == areaCodeMap.get(cyd.getCityCode())){
                           Area area = new Area();
                           area.setCode(cyd.getCityCode());
                           area.setName(cyd.getCityName());
                           if(StringUtils.isNotEmpty(cyd.getCityName())){
                               area.setPinyin(PinyinUtil.getPinyin(cyd.getCityName()));
                               area.setPre(StringUtils.upperCase(String.valueOf(PinyinUtil.getFirstLetter(cyd.getCityName().charAt(0)))));
                           }
                           area.setType(String.valueOf(AreaTypeEnum.city.getValue()));
                           area.setParentId(areaCodeMap.get(cyd.getProvinceCode()).getId());
                           areaService.insertArea(area);
                           areaCodeMap.put(cyd.getCityCode(),area);
                       }
                       if(StringUtils.isNotEmpty(cyd.getDistrictCode()) && null == areaCodeMap.get(cyd.getDistrictCode())){
                           Area area = new Area();
                           area.setCode(cyd.getDistrictCode());
                           area.setName(cyd.getDistrictName());
                           if(StringUtils.isNotEmpty(cyd.getDistrictName())){
                               area.setPinyin(PinyinUtil.getPinyin(cyd.getDistrictName()));
                               area.setPre(StringUtils.upperCase(String.valueOf(PinyinUtil.getFirstLetter(cyd.getDistrictName().charAt(0)))));
                           }
                           area.setType(String.valueOf(AreaTypeEnum.district.getValue()));
                           area.setParentId(areaCodeMap.get(cyd.getCityCode()).getId());
                           areaService.insertArea(area);
                           areaCodeMap.put(cyd.getDistrictCode(),area);
                       }

                       YzResult<CinemaYzDTO>  cinemaInfo =  YzUtils.getCinemaInfo(cyd.getCinemaLinkId());
                       if(null != cinemaInfo.getResultCode() && cinemaInfo.getResultCode().equals(SUCCESS)){
                            if(null != cinemaInfo.getData() && cinemaInfo.getData().getBizCode().equals(BIZSUCCESS)){
                                CinemaYzDTO cinemaYzDTO = cinemaInfo.getData().getBizData();
                                Cinema cinema = new Cinema();
                                BeanUtil.copyProperties(cinemaYzDTO,cinema);
                                Cinema oldCinema = cinemaService.getOne(new QueryWrapper<Cinema>().eq("cinema_link_id",cyd.getCinemaLinkId()));
                                if(null == oldCinema ){
                                    cinema.setAreaId(String.valueOf(areaCodeMap.get(cyd.getCityCode()).getId()));
                                    cinemaService.insertCinema(cinema);
                                }else {
                                    BeanUtil.copyProperties(cinema,oldCinema,"id");
                                    oldCinema.setAreaId(String.valueOf(areaCodeMap.get(cyd.getCityCode()).getId()));
                                    cinemaService.updateCinema(oldCinema);
                                }
                            }
                       }
                   }
               }
           }

       }
    }

    public void syncFilm(Boolean isLog){
        List<Cinema> cinemaList = cinemaService.selectCinemaList(new Cinema());
        for (Cinema cinema :cinemaList) {
            YzResult<FilmYzDTO> filmListResult = YzUtils.getFilmList(cinema.getCinemaLinkId(),"","",isLog);
            if(null != filmListResult && filmListResult.getResultCode().equals(SUCCESS) && filmListResult.getData().getBizCode().equals(BIZSUCCESS)){
                List<FilmYzDTO> filmYzDTOList = filmListResult.getData().getBizDataList();

                //todo 如果找不到则插入，如果找到是否需要更新
                if(null != filmYzDTOList && !filmYzDTOList.isEmpty()){
                    for (FilmYzDTO fyd :filmYzDTOList) {
                       YzResult<FilmYzDTO> filmInfoResult = YzUtils.getFilmInfo(cinema.getCinemaLinkId(),fyd.getFilmCode(),isLog);
                       if(null != filmInfoResult && filmInfoResult.getResultCode().equals(SUCCESS)){
                           fyd = filmInfoResult.getData().getBizData();
                           Film oldFilm = filmService.getOne(new QueryWrapper<Film>().eq("film_name",fyd.getFilmName()));
                           if(null == oldFilm){
                               List<String> DTOStillList = fyd.getStillList();
                               Film film = new Film();
                               BeanUtil.copyProperties(fyd,film,"id","stillList");
                               filmService.insertFilm(film);
                               FilmType filmType = new FilmType();
                               filmType.setFilmId(film.getId());
                               filmType.setFilmCode(fyd.getFilmCode());
                               filmType.setFilmVersion(fyd.getFilmVersion());
                               filmType.setLocalFilmVersion(fyd.getLocalFilmVersion());
                               filmTypeService.insertFilmType(filmType);
                               if(null != DTOStillList){
                                   List<FilmStill> filmStillList = new ArrayList<>(DTOStillList.size());
                                   for (String stillURl :DTOStillList) {
                                       FilmStill filmStill = new FilmStill();
                                       filmStill.setFilmId(film.getId());
                                       filmStill.setUrl(stillURl);
                                       filmStillList.add(filmStill);
                                   }
                                   filmStillService.saveBatch(filmStillList);
                               }
                           }else {
                               Film film = new Film();
                               BeanUtil.copyProperties(fyd,film,"id","stillList");
                               film.setId(oldFilm.getId());
                               if(!film.equals(oldFilm)){
                                  filmService.updateFilm(film);
                               }


                               FilmType filmType = new FilmType();
                               filmType.setFilmId(oldFilm.getId());
                               List<FilmType> filmTypeList = filmTypeService.selectFilmTypeList(filmType);
                               if(null == filmTypeList){
                                   filmTypeList = new ArrayList<>();
                               }
                               String oldFilmCode = fyd.getFilmCode();
                               if(filmTypeList.stream().noneMatch(ft -> ft.getFilmCode().equals(oldFilmCode))){
                                   FilmType ft = new FilmType();
                                   ft.setFilmId(oldFilm.getId());
                                   ft.setFilmCode(fyd.getFilmCode());
                                   ft.setFilmVersion(fyd.getFilmVersion());
                                   ft.setLocalFilmVersion(fyd.getLocalFilmVersion());
                                   filmTypeService.insertFilmType(ft);
                               }
                           }
                       }

                    }
                }
            }
        }
    }

    public void syncFilmSchedule(Boolean isLog){
        logger.info("++++---排期同步开始---++++");

        syncFilm(isLog);

        List<Cinema> cinemaList = cinemaService.selectCinemaList(new Cinema());
        List<FilmSchedule> scheduleList = filmScheduleService.selectFilmScheduleList(new FilmSchedule());
        Map<String,FilmSchedule> scheduleMap = scheduleList.stream().collect(Collectors.toMap(FilmSchedule::getScheduleId,filmSchedule -> filmSchedule));
        List<ScheduleYzDTO> newScheduleList = new LinkedList<>();
        if(null != cinemaList && !cinemaList.isEmpty()){
            for (Cinema cinema :cinemaList) {
                Date beginDate = new Date(System.currentTimeMillis());
                Date endDate = DateUtils.addDays(beginDate,60);
               YzResult<ScheduleYzDTO> scheduleYzDTOResult = YzUtils.getScheduleList(cinema.getCinemaLinkId(),beginDate,endDate, isLog);
               if(null != scheduleYzDTOResult && scheduleYzDTOResult.getResultCode().equals(SUCCESS) && scheduleYzDTOResult.getData().getBizCode().equals(BIZSUCCESS)){
                   List<ScheduleYzDTO> scheduleYzDTOList = scheduleYzDTOResult.getData().getBizDataList();
                      if(null != scheduleYzDTOList && !scheduleYzDTOList.isEmpty()){
                          for (ScheduleYzDTO syd :scheduleYzDTOList) {
                              syd.setCinemaId(cinema.getId());
                              newScheduleList.add(syd);
                          }
                      }
               }else {
                   //todo 这里暂时先这样，后面要把对比放在单个影院，防止因为一个影院的同步不成功，影响别的影院的同步
                   System.out.println("有个同步不成功");
                   return;
               }
            }
        }
        for (ScheduleYzDTO syd:newScheduleList) {
            if (StringUtils.isNotEmpty(syd.getScheduleId())) {
                    if (scheduleMap.containsKey(syd.getScheduleId())) {
                        FilmSchedule filmSchedule = scheduleMap.get(syd.getScheduleId());
                        BeanUtil.copyProperties(syd, filmSchedule);
                        filmSchedule.setFilmCode(syd.getFilm().getFilmCode());
                        if(null == syd.getTicketFee()){
                            filmSchedule.setTicketFee(0L);
                        }
                        filmSchedule.setLanguage(syd.getFilm().getLanguage());
                        filmSchedule.setLanguageCode(syd.getFilm().getLanguageCode());
                        filmSchedule.setDimensional(syd.getFilm().getDimensional());
                        filmSchedule.setLanguageCode(syd.getFilm().getDimensionalCode());
                        filmSchedule.setDuration(syd.getFilm().getDuration().longValue());

                        scheduleAreaInfoService.remove(new QueryWrapper<ScheduleAreaInfo>().eq("schedule_id", filmSchedule.getId()));
                        syd.getAreaInfoList().forEach(scheduleAreaInfo -> {scheduleAreaInfo.setScheduleId(filmSchedule.getId());});
                        scheduleAreaInfoService.saveBatch(syd.getAreaInfoList());
                        filmScheduleService.updateFilmSchedule(filmSchedule);
                        scheduleMap.remove(syd.getScheduleId());

                    }else {
                        FilmSchedule filmSchedule = new FilmSchedule();
                        BeanUtil.copyProperties(syd, filmSchedule);
                        Long filmId = filmService.selectIdByFilmCode(syd.getFilm().getFilmCode());
                        filmSchedule.setFilmCode(syd.getFilm().getFilmCode());
                        filmSchedule.setLanguage(syd.getFilm().getLanguage());
                        filmSchedule.setLanguageCode(syd.getFilm().getLanguageCode());
                        filmSchedule.setDimensional(syd.getFilm().getDimensional());
                        filmSchedule.setLanguageCode(syd.getFilm().getDimensionalCode());
                        filmSchedule.setDuration(syd.getFilm().getDuration().longValue());
                        filmSchedule.setFilmId(filmId);
                        filmScheduleService.insertFilmSchedule(filmSchedule);
                        syd.getAreaInfoList().forEach(scheduleAreaInfo -> {scheduleAreaInfo.setScheduleId(filmSchedule.getId());});
                        scheduleAreaInfoService.saveBatch(syd.getAreaInfoList());
                }
            }
        }

        if(!scheduleMap.isEmpty()){
            List<Long> shouldRemoveIds = new ArrayList<>(scheduleMap.size());
            for( FilmSchedule entry : scheduleMap.values()){
                shouldRemoveIds.add(entry.getId());
            }
            Long[] a = new Long[shouldRemoveIds.size()];
            scheduleAreaInfoService.remove(new QueryWrapper<ScheduleAreaInfo>().in("schedule_id",shouldRemoveIds));
            filmScheduleService.deleteFilmScheduleByIds(shouldRemoveIds.toArray(a));
        }
        logger.info("+++++----排期同步----结束---+++");

    }

    public void syncHallSeat(Boolean isLog){
        logger.info("+++----座位同步开始-----++++");
        Map<String,Map<String,String>> cinemaHallSeatMap = new HashMap<>();
        Map<String, String> scheduleAreaHallSeatMap = new HashMap<>();
        List<FilmSchedule> filmScheduleList = filmScheduleService.list(new QueryWrapper<FilmSchedule>().select("cinema_link_id","schedule_id"));
        for (FilmSchedule fs :filmScheduleList) {
            YzResult<SeatPlanYZALLDTO> seatPlanYZALLDTOResult = YzUtils.getSeatPlanList(fs.getCinemaLinkId(),fs.getScheduleId(),isLog);
            YzResult<AreaSeatPlanYZALLDTO> areaSeatPlanYZALLDTOResult =  YzUtils.getAreaSeatPlanList(fs.getCinemaLinkId(),fs.getScheduleId(),isLog);
            if(null != seatPlanYZALLDTOResult && seatPlanYZALLDTOResult.getResultCode().equals(SUCCESS) && seatPlanYZALLDTOResult.getData().getBizCode().equals(BIZSUCCESS)){

                if(!seatPlanYZALLDTOResult.getData().getBizDataList().isEmpty()){
                    SeatPlanYZALLDTO seatPlanYZALLDTO = seatPlanYZALLDTOResult.getData().getBizDataList().get(0);
                    Map<String, String> hallSeatMap = cinemaHallSeatMap.computeIfAbsent(fs.getCinemaLinkId(), k -> new HashMap<>());
                    hallSeatMap.putIfAbsent(seatPlanYZALLDTO.getHallCode(),JSON.toJSONString(seatPlanYZALLDTO));
                }

                if(null != areaSeatPlanYZALLDTOResult && areaSeatPlanYZALLDTOResult.getResultCode().equals(SUCCESS)
                        && areaSeatPlanYZALLDTOResult.getData().getBizCode().equals(BIZSUCCESS)){
                    if(!areaSeatPlanYZALLDTOResult.getData().getBizDataList().isEmpty()){
                        AreaSeatPlanYZALLDTO areaSeatPlanYZALLDTO =  areaSeatPlanYZALLDTOResult.getData().getBizDataList().get(0);
                        scheduleAreaHallSeatMap.put(fs.getScheduleId(),JSON.toJSONString(areaSeatPlanYZALLDTO));
                    }
                }

            }

        }
        for (Map.Entry<String,Map<String,String>> entry :cinemaHallSeatMap.entrySet()) {
            String cinemaLinkId = entry.getKey();
            Map<String,String> mapValue = entry.getValue();
            redisCache.deleteObject(cinemaLinkId + Constants.CINEMA_HALL);
            redisCache.setCacheMap( cinemaLinkId + Constants.CINEMA_HALL, mapValue);
            redisCache.expire(cinemaLinkId + Constants.CINEMA_HALL,3600*10);
        }
        redisCache.deleteObject(Constants.SCHEDULE_AREA_HALL_MAP);
        redisCache.setCacheMap(Constants.SCHEDULE_AREA_HALL_MAP,scheduleAreaHallSeatMap);
        redisCache.expire(Constants.SCHEDULE_AREA_HALL_MAP,3600*10);
        logger.info("++-----座位同步----结束--+++");
    }

    public void syncScheduleAndSeat(Boolean isLog){
        syncFilmSchedule(isLog);
        syncHallSeat(isLog);
    }

//    public void syncGoodsCategory(){
//        List<Cinema> cinemaList = cinemaService.selectCinemaList(new Cinema());
//        for (Cinema cinema :cinemaList) {
//
//            List<GoodsCategory> goodsCategoryList = goodsCategoryService.list(new QueryWrapper<GoodsCategory>().eq("cinema_id",cinema.getId()));
//            Map<String,GoodsCategory> map = goodsCategoryList.stream().collect(Collectors.toMap(GoodsCategory::getCategoryCode,goodsCategory -> goodsCategory));
//            YzResult<CategoryYzDTO> categoryYzDTOResult = YzUtils.getCategoryList(cinema.getCinemaLinkId());
//
//            if(null != categoryYzDTOResult && categoryYzDTOResult.getResultCode().equals(SUCCESS)){
//                List<CategoryYzDTO> categoryYzDTOList = categoryYzDTOResult.getData().getBizDataList();
//                for (CategoryYzDTO cyd :categoryYzDTOList) {
//                    if(map.containsKey(cyd.getCategoryCode())){
//                       GoodsCategory gc =  map.get(cyd.getCategoryCode());
//                       BeanUtil.copyProperties(cyd, gc, "id");
//                       goodsCategoryService.updateGoodsCategory(gc);
//                       map.remove(cyd.getCategoryCode());
//                    }else {
//                        GoodsCategory gc = new GoodsCategory();
//                        BeanUtil.copyProperties(cyd, gc, "id");
//                        goodsCategoryService.insertGoodsCategory(gc);
//                    }
//                }
//                Long[] ids = new Long[map.size()];
//                int index = 0;
//                for (GoodsCategory gc :map.values()) {
//                    ids[index] = gc.getId();
//                    index++;
//                }
//                goodsCategoryService.deleteGoodsCategoryByIds(ids);
//            }
//        }
//    }

    public void syncGoods(Boolean isLog){

        //syncGoodsCategory(isLog);

       List<Cinema> cinemaList =  cinemaService.selectCinemaList(new Cinema());
        for (Cinema cinema :cinemaList) {

            List<GoodsCategory> goodsCategoryList = goodsCategoryService.list(new QueryWrapper<GoodsCategory>().eq("cinema_id",cinema.getId()));
            Map<String,GoodsCategory> map = goodsCategoryList.stream().collect(Collectors.toMap(GoodsCategory::getCategoryCode,goodsCategory -> goodsCategory));

            List<Goods> goodsList = goodsService.list(new QueryWrapper<Goods>().eq("cinema_id",cinema.getId()));
            Map<String,Goods> goodsMap = goodsList.stream().collect(Collectors.toMap(Goods::getGoodsCode,goods -> goods));

            Map<String,GoodsCategory> insertedMap = new HashMap<>();
            Map<String,GoodsCategory> updatedMap = new HashMap<>();

            YzResult<GoodsInfoALLYzDTO> goodsInfoALLYzDTOResult = YzUtils.getGoodsList(cinema.getCinemaLinkId(),"",isLog);
            if(null != goodsInfoALLYzDTOResult && goodsInfoALLYzDTOResult.getResultCode().equals(SUCCESS) && goodsInfoALLYzDTOResult.getData().getBizCode().equals(BIZSUCCESS)){

                GoodsInfoALLYzDTO goodsInfoALLYzDTO = goodsInfoALLYzDTOResult.getData().getBizData();
                cinema.setFulfilFlag(goodsInfoALLYzDTO.getFulfilFlag());
                //todo cinema 暂时缺少 maxBuyCount
                cinemaService.updateCinema(cinema);
                List<GoodsInfoYzDTO> goodsInfoYzDTOList = goodsInfoALLYzDTO.getGoodsList();
                if(null != goodsInfoYzDTOList && !goodsInfoYzDTOList.isEmpty()){

                    for (GoodsInfoYzDTO gid :goodsInfoYzDTOList) {
                        GoodsCategory gc = null;
                        if(null != gid.getCategoryInfoRetList() && !gid.getCategoryInfoRetList().isEmpty()){

                            GoodsCategory goodsCategory = gid.getCategoryInfoRetList().get(0);
                            if(map.containsKey(goodsCategory.getCategoryCode())){
                                gc =  map.get(goodsCategory.getCategoryCode());
                                BeanUtil.copyProperties(goodsCategory, gc, "id");
                                gc.setCinemaId(cinema.getId());
                                goodsCategoryService.updateGoodsCategory(gc);
                                map.remove(goodsCategory.getCategoryCode());
                                updatedMap.put(goodsCategory.getCategoryCode(),gc);
                            }else if(updatedMap.containsKey(goodsCategory.getCategoryCode())){
                                gc = updatedMap.get(goodsCategory.getCategoryCode());
                            } else {
                                if(!insertedMap.containsKey(goodsCategory.getCategoryCode())){
                                    gc = new GoodsCategory();
                                    BeanUtil.copyProperties(goodsCategory, gc, "id");
                                    gc.setCinemaId(cinema.getId());
                                    goodsCategoryService.insertGoodsCategory(gc);
                                    insertedMap.put(goodsCategory.getCategoryCode(),gc);
                                }

                            }
                        }

                        Goods g = null;
                        if(goodsMap.containsKey(gid.getGoodsCode())){
                            g = goodsMap.get(gid.getGoodsCode());
                            BeanUtil.copyProperties(gid, g,"id");
                            if(null != gc){
                                g.setCategoryId(gc.getId());
                            }

                            goodsService.updateGoods(g);
                            goodsMap.remove(gid.getGoodsCode());
                            List<ComboGroupInfo> comboGroupInfoList = comboGroupInfoService.list(new QueryWrapper<ComboGroupInfo>().select("id").eq("goods_id",g.getId()));
                            if(null != comboGroupInfoList && !comboGroupInfoList.isEmpty()){
                                for (ComboGroupInfo cgi:comboGroupInfoList) {
                                    comboGroupGoodsService.remove(new QueryWrapper<ComboGroupGoods>().eq("combo_group_id",cgi.getId()));
                                    comboGroupInfoService.deleteComboGroupInfoById(cgi.getId());
                                }

                            }
                        }else {
                            g = new Goods();
                            if(null != gc){
                                g.setCategoryId(gc.getId());
                            }
                            BeanUtil.copyProperties(gid, g, "id");
                            g.setCinemaId(cinema.getId());
                            goodsService.insertGoods(g);
                        }

                        if(null != gid.getComboGroupInfoRetList() && !gid.getComboGroupInfoRetList().isEmpty()){
                            for (GoodsComboGroupInfo gcgi : gid.getComboGroupInfoRetList()) {
                                ComboGroupInfo comboGroupInfo = new ComboGroupInfo();
                                BeanUtil.copyProperties(gcgi, comboGroupInfo, "id");
                                comboGroupInfo.setGoodsId(g.getId());
                                comboGroupInfoService.insertComboGroupInfo(comboGroupInfo);
                                List<GroupGoodsInfoYzDTO> ggiyDList = gcgi.getGroupGoodsInfoRetList();
                                for (GroupGoodsInfoYzDTO ggiyd :ggiyDList) {
                                    if( null != ggiyd.getExtraMoney() && !ggiyd.getExtraMoney().equals(0L)){
                                        Double realExtral = ggiyd.getExtraMoney() / 100.0;
                                        ggiyd.setGoodsName(ggiyd.getGoodsName() + "+"+realExtral+"元");
                                    }
                                    ComboGroupGoods comboGroupGoods = new ComboGroupGoods();
                                    BeanUtil.copyProperties(ggiyd, comboGroupGoods, "id");
                                    comboGroupGoods.setComboGroupId(comboGroupInfo.getId());
                                    comboGroupGoodsService.insertComboGroupGoods(comboGroupGoods);
                                }
                            }
                        }

                    }
                }

                Long[] gcids = new Long[map.size()];
                int index = 0;
                for (GoodsCategory gc :map.values()) {
                    gcids[index] = gc.getId();
                    index++;
                }
                if(gcids.length>0){
                    goodsCategoryService.deleteGoodsCategoryByIds(gcids);
                }


                Long[] gids = new Long[goodsMap.size()];
                index = 0;
                for (Goods g :goodsMap.values()) {
                    gids[index] = g.getId();
                    index++;
                }
                if(gids.length > 0){
                    goodsService.deleteGoodsByIds(gids);
                }



            }

        }

    }

    public void syncRefreshToken(){
        YzUtils.getRefreshToken();
    }

    public void syncSchedulePromo(){
        List<Cinema> cinemaList = cinemaService.selectCinemaList(new Cinema());
        schedulePromoService.remove(new QueryWrapper<>());
        schedulePromoCardInfoService.remove(new QueryWrapper<>());
        schedulePromoAreaService.remove(new QueryWrapper<>());
        for (Cinema cinema:cinemaList) {
            FilmSchedule filmSchedule = new FilmSchedule();
            filmSchedule.setCinemaId(cinema.getId());
            List<FilmSchedule> filmScheduleList = filmScheduleService.selectFilmScheduleList(filmSchedule);
            List<String> filmScheduleIdList = filmScheduleList.stream().map(FilmSchedule::getScheduleId).collect(Collectors.toList());
            if(!filmScheduleIdList.isEmpty()){
                YzResult<SchedulePromoYzDTO> schedulePromoYzDTOYzResult = YzUtils.getSchedulePromoList(cinema.getCinemaLinkId(),filmScheduleIdList,"");
                if(schedulePromoYzDTOYzResult.getResultCode().equals(SUCCESS) && schedulePromoYzDTOYzResult.getData().getBizCode().equals(BIZSUCCESS)){
                    List<SchedulePromoYzDTO> schedulePromoYzDTOList = schedulePromoYzDTOYzResult.getData().getBizDataList();
                    if(null != schedulePromoYzDTOList && !schedulePromoYzDTOList.isEmpty()){
                        for (SchedulePromoYzDTO sfyd :schedulePromoYzDTOList) {
                            SchedulePromo schedulePromo = new SchedulePromo();
                            BeanUtil.copyProperties(sfyd, schedulePromo, "id");
                            schedulePromoService.insertSchedulePromo(schedulePromo);
                            List<AreaPromoYzDTO> areaPromoList = sfyd.getAreaPromoList();
                            if(null != areaPromoList && !areaPromoList.isEmpty()){
                                for (AreaPromoYzDTO apyd :areaPromoList) {
                                    SchedulePromoArea schedulePromoArea = new SchedulePromoArea();
                                    BeanUtil.copyProperties(apyd,schedulePromoArea, "id");
                                    schedulePromoArea.setSchedulePromoId(schedulePromo.getId());
                                    schedulePromoAreaService.insertSchedulePromoArea(schedulePromoArea);
                                }
                            }

                            List<PromoCardInfoYzDTO> promoCardInfoList = sfyd.getPromoCardInfoList();
                            if(null != promoCardInfoList && !promoCardInfoList.isEmpty()){
                                for (PromoCardInfoYzDTO pciyd :promoCardInfoList) {
                                    SchedulePromoCardInfo schedulePromoCardInfo = new SchedulePromoCardInfo();
                                    BeanUtil.copyProperties(pciyd, schedulePromoCardInfo, "id");
                                    schedulePromoCardInfo.setSchedulePromoId(schedulePromo.getId());
                                    schedulePromoCardInfoService.insertSchedulePromoCardInfo(schedulePromoCardInfo);
                                }
                            }
                        }
                    }

                }

            }

        }
    }

    public void delUnPayOrder(){

    }
}
